package com.sicheng.lc.周赛.分类.设计;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/23 18:36
 */
@SuppressWarnings("all")
public class 设计一个ATM机器 {
    class ATM {
        //https://leetcode.cn/problems/design-an-atm-machine/
        // 20 50 100 200 500
        long[] balance = new long[5];
        int[] amount = {20, 50, 100, 200, 500};
        int[] res1 = {-1};
        int[] res2 = {0, 0, 0, 0, 0};
        long sum;


        public ATM() {
        }

        public void deposit(int[] banknotesCount) {
            for (int i = 0; i < banknotesCount.length; i++) {
                if (banknotesCount[i] > 0) {
                    sum += (long) amount[i] * banknotesCount[i];
                    balance[i] += banknotesCount[i];
                }
            }
        }

        public int[] withdraw(int amount) {
            if (amount > sum)
                return res1;

            int t = 4;
            Arrays.fill(res2, 0);
            while (amount > 0 && t >= 0) {
                int x = 0;
                if (amount >= this.amount[t]) {
                    x = (int) Math.min(amount / this.amount[t], balance[t]);
                    res2[t] = x;
                }
                amount -= x * this.amount[t];
                t--;
            }
            if (amount != 0)
                return res1;
            sum -= amount;

            for (int i = 0; i < res2.length; i++) {
                balance[i] -= res2[i];
            }
            return res2;
        }
    }

}
